package com.example.passport.service;

import com.example.interceptor.UserContext;
import com.example.passport.dto.MenuSearchDto;
import com.example.passport.dto.MenuUpdateDto;
import com.example.passport.entity.Menu;
import com.example.passport.entity.User;
import com.example.passport.mapper.MenuMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuService {
    @Resource
    MenuMapper menuMapper;

    public int Permission( Long userId,String url){
        return menuMapper.isPermission(userId,url);
    }

    public List<Menu> findMenu(MenuSearchDto menu){
        User user=UserContext.getUser();
        menu.setUserId(user.getId());
        return menuMapper.findAll(menu);
    }
    public List<Menu> findAll(){
        return menuMapper.find();
    }
    public List<Long> findId(Long roleId){
        return menuMapper.findId(roleId);
    }
    public void update(MenuUpdateDto menuUpdateDto){
       menuMapper.deleteId(menuUpdateDto.getRoleId());
       for (Long menuId : menuUpdateDto.getMenuIds()) {
           menuMapper.createRoleMenu(menuUpdateDto.getRoleId(),menuId);
       }
    }

}
